<template>
  <div class="pre">
    <template v-if="state === 1">
      <p>我的服务</p>
      <div class="wrap">
        <div class="evaluate" v-for="(item, i) in preInfo" :key="i">
          <!-- 确保处方单类型是 非图片处方 -->
          <div v-if="item.type == 0"> 
            <div class="fl">
              <img v-if="item.errorImg" :src="item.inquiry_avatar" @error="item.errorImg = false" />
              <img v-else src="@/assets/image/Furtherconsultation_Defaulthead@2x.png" />
            </div>
            <div class="fr">
              <p>
                用户姓名
                <span class="name">{{ item.inquiry_info.realname }}</span>
                <span
                  class="sex"
                >{{ item.inquiry_info.sex === 0 ? "保密" : (item.inquiry_info.sex === 1 ? "男" : "女")}}</span>
                <span class="age">{{ item.inquiry_info.age }}</span>
                电话 :
                <span class="phone">{{ item.inquiry_info.mobile }}</span>
              </p>
              <p>
                科别 :
                <span>{{ item.inquiry_info.category_name}}</span>
              </p>
              <p>
                诊断结果 :
                <span>{{ item.inquiry_info.diagnosis }}</span>
              </p>
              <p class="details">
                <span @click="changeState(2, item)">查看详情</span>
              </p>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-if="state === 2">
      <div class="details-wrap">
        <p>查看服务详情</p>
        <div class="wrap">
          <section>
            <div class="title">
              <img
                v-if="detailPre.errorImg"
                :src="detailPre.inquiry_avatar"
                @error="detailPre.errorImg = false"
              />
              <img v-else src="@/assets/image/Furtherconsultation_Defaulthead@2x.png" />
              <dl class="fr">
                <dd>
                  <span class="name">{{ detailPre.inquiry_info.realname }}</span>
                  <span>{{ detailPre.inquiry_info.sex === 0 ? "保密" : (detailPre.inquiry_info.sex === 1 ? "男" : "女" )}}</span>
                  <span>{{ detailPre.inquiry_info.age }}</span>
                </dd>
                <dd>
                  <span>科别:</span>
                  <span>{{ detailPre.inquiry_info.category_name }}</span>
                </dd>
                <dd>
                  <span>临床诊断:</span>
                  <span>{{ detailPre.inquiry_info.diagnosis && "无"}}</span>
                </dd>
              </dl>
            </div>
            <button @click="changeState(3)">处方单</button>
          </section>

          <div class="content">
            <dl>
              <dd>
                <span>姓名</span>
                <span>{{ detailPre.inquiry_info.realname }}</span>
              </dd>
              <dd>
                <span>年龄</span>
                <span>{{ detailPre.inquiry_info.age }}</span>
              </dd>
              <dd>
                <span>性别</span>
                <span>{{ detailPre.inquiry_info.sex === 0 ? "保密" : (detailPre.inquiry_info.sex === 1 ? "男" : "女" )}}</span>
              </dd>
              <dd>
                <span>体重</span>
                <span>{{ detailPre.inquiry_info.weight }}</span>
              </dd>
              <dd>
                <span>身高</span>
                <span>{{ detailPre.inquiry_info.height }}</span>
              </dd>
              <dd>
                <span>手机号</span>
                <span>{{ detailPre.inquiry_info.mobile }}</span>
              </dd>
              <dd>
                <span>过敏史</span>
                <span>{{ detailPre.inquiry_info.allergy }}</span>
              </dd>
              <dd>
                <span>遗传史</span>
                <span>{{ detailPre.inquiry_info.genetic_disease }}</span>
              </dd>
              <dd>
                <span>疾病情况</span>
                <span>{{ detailPre.inquiry_info.diagnosis }}</span>
              </dd>
              <dd>
                <span>上传体检报告</span>
                <span>无</span>
              </dd>
            </dl>
          </div>
        </div>
      </div>
    </template>
    <template v-if="state === 3">
      <div class="disponseInfo">
        <div class="bill">
          <div class="date">{{ detailPre.add_time}}</div>
          <div class="invald">本处方单3个月内有效</div>
          <h3>处方笺</h3>
          <el-row>
            <el-col :span="12">
              科别:
              <span>{{ detailPre.inquiry_info.category_name }}</span>
            </el-col>
            <el-col :span="12">
              处方号:
              <span>{{ detailPre.p_code }}</span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              姓名:
              <span>{{ detailPre.inquiry_info.realname }}</span>
            </el-col>
            <el-col :span="8">
              年龄:
              <span>{{ detailPre.inquiry_info.age }}</span>
            </el-col>
            <el-col :span="8">
              性别:
              <span>{{ detailPre.inquiry_info.sex === 0 ? "保密" : (detailPre.inquiry_info.sex === 1 ? "男" : "女" )}}</span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              过敏史:
              <span>{{ detailPre.inquiry_info.allergy }}</span>
            </el-col>
            <el-col :span="12">
              遗传史:
              <span>{{ detailPre.inquiry_info.allergy.genetic_disease}}</span>
            </el-col>
          </el-row>
          <el-row>
            临床诊断:
            <span>{{ detailPre.inquiry_info.allergy.diagnosis }}</span>
          </el-row>
          <div class="content">
            <div class="type">R</div>
            <div class="name" v-for="(item, i) in detailPre.goods_list" :key="i">
              <el-row>
                <el-col :span="8">{{ item.goods_name }}</el-col>
                <el-col :span="8">10g:0.5mg</el-col>
                <el-col :span="8">x{{ item.num }}{{ item.unit }}</el-col>
                <el-col :span="8">用法 内服</el-col>
                <el-col :span="8">{{ item.yongfa }} {{item.yongliang}}</el-col>
                <el-col :span="8">每次0.1g</el-col>
              </el-row>
            </div>
          </div>

          <div class="graph">
            <el-row>
              <el-col :span="12">
                <p>医生签字:</p>
                <img :src="doctorSign && doctorSign.audit_sign" />
              </el-col>
              <el-col :span="12">
                <p>药师签字:</p>
                <img :src="pharmSign && pharmSign.audit_sign" />
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
// 获取自己添加的处方单
import { getPrescriptionList } from "@/request/personnel";
// 获取处方单详情
import { getInfo } from "@/request/personnel";

export default {
  data() {
    return {
      state: 1,
      prelist: [],
      preInfo: {}, // 处方单详情信息，key是处方单号，value是处方单详细信息
      errorImg: true, // 图片是否加载成功
      detailPre: {}, // 当前处方详情对象
      doctorSign: {}, // 医生或专家的签名对象
      pharmSign: {} // 药师的签名对象
    };
  },
  created() {
    getPrescriptionList({
      token: this.$token,
      status_str: "1,2"
    })
      .then(res => {
        this.prelist = res.data.data;
        console.log(this.prelist);
        return this.prelist;
      })
      .then(res => {
        let items = res.map(item =>
          getInfo({
            token: this.$token,
            p_code: item.p_code
          }).then(res => {
            res.data.errorImg = true;
            this.preInfo[item.p_code] = res.data;
          })
        );
        Promise.all(items).then(() => {
          // 此时所有数据都已经获取完成
          this.preInfo = { ...this.preInfo };
          console.log(this.preInfo);
        });
      });
  },
  methods: {
    changeState(num, item) {
      this.state = num;
      if (num === 2) {
        this.detailPre = item;
        this.doctorSign = this.detailPre.sign_list.find(
          item => item.cert_type == 1 || item.cert_type == 2
        );
        this.pharmSign = this.detailPre.sign_list.find(
          item => item.cert_type == 3
        );
      }
    }
  }
};
</script>

<style lang="less" scoped>
.pre {
  width: 1200px;
  margin: 0 auto;
  min-height: 830px;
  margin-top: 10px;
  font-size: 16px;
}
.wrap {
  background: #fff;
}
.pre > p,
.details-wrap > p {
  background: #fff;
  font-size: 20px;
  color: #000;
  height: 70px;
  border: 1px solid #ccc;
  border-bottom: 0px;
  line-height: 70px;
  padding-left: 15px;
}
.details-wrap > p {
  color: #4eeec8;
}
.evaluate:after {
  content: "";
  display: block;
  clear: both;
}
.wrap {
  height: 690px;
  border: 1px solid #ccc;
}
.evaluate {
  font-size: 12px;
  margin-top: 35px;
  padding-left: 30px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ccc;
}
.evaluate .fr {
  float: left;
  span {
    margin-right: 8px;
  }
}
.evaluate .fr p {
  color: #555;
  font-size: 12px;
  height: 30px;
  margin-left: 30px;
}
.evaluate .fl img {
  width: 80px;
  height: 80px;
}
.details span {
  color: #4eeec8;
  cursor: pointer;
}
.details-wrap {
  .wrap {
    section {
      position: relative;
      overflow: hidden;
      border-bottom: 1px solid #ccc;
      button {
        padding: 6px 20px;
        background: #fff;
        color: #4eeec8;
        border: 1px solid #4eeec8;
        position: absolute;
        right: 60px;
        bottom: 20px;
        cursor: pointer;
      }
    }
    .title {
      padding-top: 30px;
      img {
        margin-left: 30px;
        width: 100px;
        height: 100px;
      }
      dl {
        dd {
          margin-bottom: 14px;
          span {
            margin-right: 6px;
          }
        }
        dd:nth-child(1) {
          span {
            margin-right: 20px;
          }
        }
      }

      width: 320px;
      height: 160px;
      .name {
        color: #4eeec8;
      }
    }
    .content {
      padding-top: 15px;
      height: 45px;
      line-height: 45px;
      font-size: 18px;
      span {
        display: inline-block;
        width: 120px;
      }
      span:nth-child(1) {
        margin-left: 50px;
        color: #313131;
      }
      span:nth-child(2) {
        color: #a8a8a8;
      }
    }
  }
}
.disponseInfo {
  width: 1200px;
  color: #333;
  margin: 0 auto;
  border: 1px solid #ccc;
  padding: 100px 0px;
  background: #fff;
}
.disponseInfo > div {
  padding: 30px;
}

// 单据信息
.disponseInfo .bill {
  box-sizing: border-box;
  background: #f5f5f5;
  margin-left: 200px;
  box-shadow: 5px 5px 5px #ccc;
  width: 600px;
  min-height: 744px;
  font-size: 16px;
  border: 1px solid #ccc;
  padding: 20px;
  .date {
    margin-bottom: 5px;
  }
  h3 {
    font-size: 28px;
    font-weight: 500;
    text-align: center;
    margin: 10px 0px;
  }
  > .el-row {
    border-bottom: 1px solid #424242;
    line-height: 40px;
  }
  .content {
    min-height: 100px;
    padding: 10px 0px 15px 0px;
    .el-row {
      border: none;
      padding: 3px 0;
      .el-col {
        margin: 3px 0;
      }
      .el-col:nth-child(2) {
        text-align: center;
      }
      .el-col:nth-child(4) {
        text-indent: 2em;
      }
      .el-col:nth-child(5) {
        text-align: center;
      }
      .el-col:nth-child(3) {
        text-align: right;
      }
      .el-col:nth-child(6) {
        text-align: right;
      }
      .el-col {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
    }
    .type {
      font-size: 22px;
      font-weight: 700;
    }
    .name {
      margin: 3px 0;
    }
  }
  .graph {
    border-top: 1px solid #424242;
    height: 150px;
    .el-row {
      margin-top: 12px;
      img {
        width: 100%;
      }
    }
  }
}
.disponseInfo .autograph {
  height: 100px;
  line-height: 100px;
  text-align: center;
  font-size: 13px;
  color: #999;
  margin-left: -25px;
  cursor: pointer;
}
.disponseInfo div.wrap_autograph {
  width: 800px;
  margin-top: 15px;
  h3 {
    margin-bottom: 15px;
    font-weight: 500;
    margin-left: 20px;
  }
  textarea {
    margin-bottom: 10px;
    font-size: 18px;
    padding: 8px;
  }
}
.disponseInfo div.wrap_autograph div {
  text-align: center;
}
.el-button.el-button--primary {
  background-color: #38eec1;
  border: #38eec1;
}
</style>